package cn.enilu.guns.service.door;


import cn.enilu.guns.bean.dto.UserDto;
import cn.enilu.guns.bean.entity.door.EntranceGardUser;
import cn.enilu.guns.bean.entity.door.UserCard;
import cn.enilu.guns.bean.entity.system.User;
import cn.enilu.guns.bean.vo.query.SearchFilter;
import cn.enilu.guns.dao.door.EntranceGardUserRepository;

import cn.enilu.guns.service.BaseService;
import cn.enilu.guns.service.system.UserService;
import cn.enilu.guns.utils.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EntranceGardUserService extends BaseService<EntranceGardUser,Long,EntranceGardUserRepository>  {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private EntranceGardUserRepository entranceGardUserRepository;
    @Autowired
    private UserService userService;
    @Autowired
    private UserCardService cardService;
    /**
     * 方法描述:通過權限id獲取用戶和卡信息
     *
     * @Author: pxb
     * @CreateDate: 2019/9/21  上午 09:00
     * @Return
     **/
    public List<UserCard> getUserAndCardInfoByRuleId(Long ruleId) {
        //查詢權限信息
        List<SearchFilter> searchFilters = Lists.newArrayList();
        searchFilters.add(new SearchFilter("ruleId",SearchFilter.Operator.EQ,ruleId));
        List<EntranceGardUser> entranceGardUsers = this.queryAll(searchFilters);
        //查詢人員和卡信息
        List<UserCard> userCardList = new ArrayList<>();
        for (EntranceGardUser gardUser : entranceGardUsers) {
            SearchFilter filter = new SearchFilter("userInfo.id",SearchFilter.Operator.EQ,gardUser.getUserId());
            SearchFilter filterIsUnbinding = new SearchFilter("isUnbinding", SearchFilter.Operator.EQ,"1");
            List<SearchFilter> filters = new ArrayList<>();
            filters.add(filter);
            filters.add(filterIsUnbinding);
            List<UserCard> userCard = cardService.queryAll(filters);
            for (UserCard card : userCard) {
                if (card != null) {
                    userCardList.add(card);
                }
            }
        }
        return userCardList;
    }


    public List<UserCard> getUserAndCardInfoByUserList(List<User> userList) {
        //查詢人員和卡信息
        List<UserCard> userCardList = new ArrayList<>();
        for (User user : userList) {
            SearchFilter filter = new SearchFilter("userInfo.id",SearchFilter.Operator.EQ,user.getId());
            SearchFilter filterIsUnbinding = new SearchFilter("isUnbinding", SearchFilter.Operator.EQ,"1");
            List<SearchFilter> filters = new ArrayList<>();
            filters.add(filter);
            filters.add(filterIsUnbinding);
            List<UserCard> userCard = cardService.queryAll(filters);
            for (UserCard card : userCard) {
                if (card != null) {
                    userCardList.add(card);
                }
            }
        }
        return userCardList;
    }
}

